<?php
//分页类
//分页最终效果 例子：[1]...[2][3][4][5(当前)][6][7][8]...[9]
class Page {
	private $total;                   //总记录条数
	private $pagesize;				  //每页显示多少条
	private $limit;					  //sql语句的显示控制(借给model层的sql语句使用)
	private $page;                    //当前页
	private $pagenum;                //显示的总页数
	private $url;                    //页面当前的url
	private $bothnum;                //当前页显示的两边的页码数，如果是3       [1][2][3][4(当前)][5][6][7]

	public function __construct($total,$pagesize) {
		$this->total = $total ? $total : 1;
		$this->pagesize = $pagesize;
		$this->pagenum = ceil($this->total / $this->pagesize);
		$this->page = $this->setPage();
		$this->url = $this->setUrl();
		$this->limit = ($this->page-1)*$this->pagesize.",".$this->pagesize;
		$this->bothnum = 2;
	}

	//动态设置当前url
	private function setUrl() {
		//获得当前url
		$url = $_SERVER['REQUEST_URI'];
		//为了不让url字段累加，要把当前的&page变量给清空
		$par = parse_url($url);
		if(isset($par['query'])) {
			//把参数解析成一个数组
			parse_str($par['query'],$query);
			//把索引是page的变量给清除掉
			unset($query['page']);
			//重组url，让其url不会连接上上一次点击的page，变成形如?action=show的形式
			$url = $par['path'].'?'.http_build_query($query);
		}
		return $url;
	}

	//设置当前页
	private function setPage() {
		if(!empty($_GET['page'])) {
			if($_GET['page'] > 0) {
				//是否大于总页数
				if($_GET['page'] > $this->pagenum) {
					return ceil($this->pagenum);
				} else {
					return intval($_GET['page']);
				}
			} else {
				return 1;
			}
		} else {
			return 1;
		}
	}

	//数字分页(需要理解)
	private function pageList() {
		$pagelist = '';
	    for($i = $this->bothnum; $i >= 1; $i--) {
			$page = $this->page - $i;
			//防止出现负数页码或者页码0
			if($page < 1) continue;
			$pagelist .= '<a href="'.$this->url.'&page='.$page.'">'.$page.'</a>';
		}
		//当前页
		$pagelist .= '<span class="me">'.$this->page.'</span>';
		for($i = 1; $i <= $this->bothnum;$i++) {
			$page = $this->page + $i;
			if($page > $this->pagenum) break;
			$pagelist .= '<a href="'.$this->url.'&page='.$page.'">'.$page.'</a>';
		}
		return $pagelist;
	}

	//文本分页
	//文本首页显示
	private function first() {
		if($this->page - $this->bothnum > 1) {
			return ' <a href="'.$this->url.'">1</a>... ';
		}
	}
	//上一页
	private function prev() {
		if($this->page == 1) {
			return '<span class="disabled">上一页</span>';
		}
		return ' <a href="'.$this->url.'&page='.($this->page - 1).'">上一页</a> ';
	}
	//下一页
	private function next() {
		if($this->page == $this->pagenum) {
			return '<span class="disabled">下一页</span>';
		}
		return ' <a href="'.$this->url.'&page='.($this->page + 1).'">下一页</a> ';
	}
	//尾页
	private function last() {
		if($this->pagenum - $this->page > $this->bothnum) {
			return ' ...<a href="'.$this->url.'&page='.$this->pagenum.'">'.$this->pagenum.'</a> ';
		}
	}

	//分页跳转
// 	private function pageLocation() {
// 		$html = '<select style="width:62px;height:21px;text-align:center" id="pagejump" onchange="pageJump(\''.$this->url.'\');">';
// 		//记录当前页码,用于js选项选定
// 		for($i = 0; $i < $this->pagenum; $i++) {
// 			$html .= '<option value="'.($i+1).'">第'.($i+1).'页</option>';
// 		}
// 		$html .= '</select>';
// 		$html .= '<input id="currentpage" type="hidden" value="'.$this->page.'" />';
// 		return $html;
// 	}

	//显示分页信息
	public function showPage() {
	    $page = '';
		$page .= $this->first();
		$page .= $this->pageList();
		$page .= $this->last();
		$page .= $this->prev();
		$page .= $this->next();
		return $page;
	}

	//获取limit
	public function getLimit() {
	    return $this->limit;
	}

	//获取当前分页
	public function getPage() {
	    return isset($_GET['page']) ? $_GET['page'] : 1;
	}
}


?>